﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>MsgBox - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The MsgBox function displays the specified text in a small window containing one or more buttons (such as Yes and No)." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
<style type="text/css">
  .fopt {
    width: 4em;
    text-align: center
  }
  .fnum {
    width: 4em;
    text-align: right
  }
</style>
</head>
<body>

<h1>MsgBox</h1>

<p>在含有一个或多个按钮(例如"是"和"否") 的小窗口中显示指定的文本.</p>

<pre class="Syntax"><span class="func">MsgBox</span> <span class="optional">Text, Title, Options</span>
Result := <span class="func">MsgBox</span>(<span class="optional">Text, Title, Options</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Text</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果省略所有的参数, 则 MsgBox 会显示 &quot;Press OK to continue.&quot;. 否则, 此参数表示消息框中显示的文本, 用来引导用户如何操作或提示信息.</p>
    <p>可以使用<a href="../misc/EscapeChar.htm">转义序列</a>来表示特殊字符. 例如, `n 表示换行符, 它结束当前行并开始一个新行. 因此, 使用 text1`n`ntext2 会在 text1 和 text2 之间添加一个空行.</p>
    <p>如果 <em>Text</em> 较长, 可以通过<a href="../Scripts.htm#continuation">延续片段</a>的方法将其分解成较短的几行, 这样可以增加可读性和可维护性.</p>
  </dd>

  <dt>Title</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>消息框窗口的标题. 如果省略, 那么它默认为 <a href="../Variables.htm#ScriptName">A_ScriptName</a> 的当前值.</p>
  </dd>

  <dt>Options</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>表示消息框的类型和可能的按钮组合. 如果为空或省略, 则默认为 0. 请参阅下面的表格来了解允许的值.</p>
  </dd>

</dl>

<h2 id="vftop"><em>Options</em> 参数的值</h2>
<p><em>Options</em> 参数可以是以下组的数字值的组合(和), 这些值直接传递给操作系统的 MessageBox 函数, 也可以是由至少一个空格或制表符分隔的不区分大小写的选项组成的字符串. 字符串中还可以包含一个或多个数字选项.</p>

<h3 id="Group_1_Buttons">Group #1: Buttons</h3>
<p>若要指定在消息框中显示的按钮, 请添加以下值 <strong>之一</strong>:</p>
<table class="info">
  <tr>
    <th>字符串</th>
    <th>十进制</th>
    <th>十六进制</th>
    <th style="text-align:left">描述</th>
  </tr>
  <tr>
    <td class="fopt">O</td>
    <td class="fnum">0</td>
    <td class="fnum">0x0</td>
    <td>仅显示一个 OK 按钮. 也可以使用 OK 来代替 O.</td>
  </tr>
  <tr>
    <td class="fopt">OC</td>
    <td class="fnum">1</td>
    <td class="fnum">0x1</td>
    <td>Displays an OK and Cancel button. Instead of OC, you can also use O/C or OKCancel.</td>
  </tr>
  <tr>
    <td class="fopt">ARI</td>
    <td class="fnum">2</td>
    <td class="fnum">0x2</td>
    <td>Displays an Abort, Retry and Ignore button. Instead of ARI, you can also use A/R/I or AbortRetryIgnore.</td>
  </tr>
  <tr>
    <td class="fopt">YNC</td>
    <td class="fnum">3</td>
    <td class="fnum">0x3</td>
    <td>Displays a Yes, No and Cancel button. Instead of YNC, you can also use Y/N/C or YesNoCancel.</td>
  </tr>
  <tr>
    <td class="fopt">YN</td>
    <td class="fnum">4</td>
    <td class="fnum">0x4</td>
    <td>Displays a Yes and No button. Instead of YN, you can also use Y/N or YesNo.</td>
  </tr>
  <tr>
    <td class="fopt">RC</td>
    <td class="fnum">5</td>
    <td class="fnum">0x5</td>
    <td>Displays a Retry and Cancel button. Instead of RC, you can also use R/C or RetryCancel.</td>
  </tr>
  <tr>
    <td class="fopt">CTC</td>
    <td class="fnum">6</td>
    <td class="fnum">0x6</td>
    <td>Displays a Cancel, Try Again and Continue button. Instead of CTC, you can also use C/T/C or CancelTryAgainContinue.</td>
  </tr>
</table>

<h3 id="Group_2_Icon">Group #2: Icon</h3>
<p>To display an icon in the message box, add <strong>one</strong> of the following values:</p>
<table class="info">
  <tr>
    <th>字符串</th>
    <th>十进制</th>
    <th>十六进制</th>
    <th style="text-align:left">描述</th>
  </tr>
  <tr>
    <td class="fopt">Iconx</td>
    <td class="fnum">16</td>
    <td class="fnum">0x10</td>
    <td>Displays a hand icon which usually indicates stop or error.</td>
  </tr>
  <tr>
    <td class="fopt">Icon?</td>
    <td class="fnum">32</td>
    <td class="fnum">0x20</td>
    <td>Displays a question icon.</td>
  </tr>
  <tr>
    <td class="fopt">Icon!</td>
    <td class="fnum">48</td>
    <td class="fnum">0x30</td>
    <td>Displays an exclamation icon.</td>
  </tr>
  <tr>
    <td class="fopt">Iconi</td>
    <td class="fnum">64</td>
    <td class="fnum">0x40</td>
    <td>Displays an asterisk icon which usually indicates info.</td>
  </tr>
</table>

<h3 id="Group_3_Default_Button">Group #3: Default Button</h3>
<p>To indicate the default button, add <strong>one</strong> of the following values:</p>
<table class="info">
  <tr>
    <th>字符串</th>
    <th>十进制</th>
    <th>十六进制</th>
    <th style="text-align:left">描述</th>
  </tr>
  <tr>
    <td class="fopt">Default2</td>
    <td class="fnum">256</td>
    <td class="fnum">0x100</td>
    <td>Makes the 2nd button the default button.</td>
  </tr>
  <tr>
    <td class="fopt">Default3</td>
    <td class="fnum">512</td>
    <td class="fnum">0x200</td>
    <td>Makes the 3rd button the default button.</td>
  </tr>
  <tr>
    <td class="fopt">Default4</td>
    <td class="fnum">768</td>
    <td class="fnum">0x300</td>
    <td>Makes the 4th button the default button. Requires the <a href="#Help">Help button</a> to be present.</td>
  </tr>
</table>

<h3 id="Group_4_Modality">Group #4: Modality</h3>
<p>To indicate the modality of the dialog box, add <strong>one</strong> of the following values:</p>
<table class="info">
  <tr>
    <th>字符串</th>
    <th>十进制</th>
    <th>十六进制</th>
    <th style="text-align:left">描述</th>
  </tr>
  <tr>
    <td class="fopt">&nbsp;</td>
    <td class="fnum">4096</td>
    <td class="fnum">0x1000</td>
    <td>System Modal (always on top)</td>
  </tr>
  <tr>
    <td class="fopt">&nbsp;</td>
    <td class="fnum">8192</td>
    <td class="fnum">0x2000</td>
    <td>Task Modal</td>
  </tr>
  <tr>
    <td class="fopt">&nbsp;</td>
    <td class="fnum">262144</td>
    <td class="fnum">0x40000</td>
    <td>Always-on-top (style WS_EX_TOPMOST) (like System Modal but omits title bar icon)</td>
  </tr>
</table>

<h3 id="Group_5_Other_Options">Group #5: Other Options</h3>
<p>To specify other options, add <strong>one or more</strong> of the following values:</p>
<table class="info">
  <tr>
    <th>字符串</th>
    <th>十进制</th>
    <th>十六进制</th>
    <th style="text-align:left">描述</th>
  </tr>
  <tr id="Help">
    <td class="fopt">&nbsp;</td>
    <td class="fnum">16384</td>
    <td class="fnum">0x4000</td>
    <td>
      <p>Adds a Help button. Pressing this button will have no effect unless both of the following are true:</p>
      <ol>
        <li>The MsgBox is owned by a GUI window by means of the <a href="../objects/Gui.htm#OwnDialogs">OwnDialogs option</a>.</li>
        <li>The script is monitoring the WM_HELP message (0x53). For example: <code><a href="OnMessage.htm">OnMessage</a>(0x53, &quot;WM_HELP&quot;)</code>. When the WM_HELP function is called, it may guide the user by means such as showing another window or MsgBox.</li>
      </ol>
    </td>
  </tr>
  <tr>
    <td class="fopt">&nbsp;</td>
    <td class="fnum">524288</td>
    <td class="fnum">0x80000</td>
    <td>Makes the text right-justified.</td>
  </tr>
  <tr>
    <td class="fopt">&nbsp;</td>
    <td class="fnum">1048576</td>
    <td class="fnum">0x100000</td>
    <td>Enables right-to-left reading order for Hebrew/Arabic.</td>
  </tr>
    <tr id="OwnerOption">
      <td class="fopt">Owner<em>HWND</em></td>
      <td class="fnum">&nbsp;</td>
      <td class="fnum">&nbsp;</td>
      <td>To specify an <a href="#Owner">owner window</a> for the MsgBox, use the word <code>Owner</code> followed immediately by a HWND (window ID).</td>
    </tr>
  <tr id="Timeout">
    <td class="fopt">T<em>Seconds</em></td>
    <td class="fnum">&nbsp;</td>
    <td class="fnum">&nbsp;</td>
    <td>
      <p>(Timeout) To have the MsgBox close automatically if the user has not closed it within a specified time, use the letter <code>T</code> followed by the timeout in seconds, which can contain a decimal point. If this value exceeds 2147483 (24.8 days), it will be set to 2147483.</p>
      <p>If the MsgBox times out, the <a href="#Result">return value</a> is the word <code>Timeout</code>.</p>
    </td>
  </tr>
</table>

<h2 id="Result">返回值</h2>
<p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
<p>When called from an expression, MsgBox returns one of the following strings to represent which button the user pressed:</p>
<p>OK<br>
Cancel<br>
Yes<br>
No<br>
Abort<br>
Retry<br>
Ignore<br>
TryAgain<br>
Continue<br>
Timeout (that is, the word "timeout" is returned if the MsgBox timed out)</p>

<h2 id="Remarks">备注</h2>
<p>A message box usually looks like this:</p>
<img src="../static/dlg_message.png" alt="MsgBox" />
<p>To determine which button the user pressed, use the function's <a href="#Result">return value</a>. For example:</p>
<pre>Result := MsgBox("Would you like to continue? (press Yes or No)",, "YesNo")
if Result = "Yes"
    MsgBox "You pressed Yes."
else
    MsgBox "You pressed No."

if MsgBox("Retry or cancel?",, "R/C") = "Retry"
    MsgBox("You pressed Retry.")</pre>
<p>The names of the buttons can be customized by following <a href="../scripts/MsgBoxButtonNames.htm">this example</a>.</p>
<p><strong>Tip</strong>: Pressing <kbd>Control</kbd>+<kbd>C</kbd> while a MsgBox window is active will copy its text to the clipboard. This applies to all MsgBoxes, not just those produced by AutoHotkey.</p>
<p id="Owner"><strong>Using MsgBox with GUI windows</strong>: A GUI window may display a <em>modal</em> MsgBox by means of the <a href="../objects/Gui.htm#OwnDialogs">OwnDialogs option</a>. A <em>modal</em> MsgBox prevents the user from interacting with the GUI window until the MsgBox is dismissed. In such a case, it is not necessary to specify the System Modal or Task Modal options from the table above.</p>
<p>When the <a href="../objects/Gui.htm#OwnDialogs">OwnDialogs option</a> is <em>not</em> in effect, the Task Modal option (8192) can be used to disable all the script's windows until the user dismisses the MsgBox.</p>
<p>If the <code>Owner<i>HWND</i></code> option is specified, it takes precedence over any other setting. <i>HWND</i> can be the HWND of any window, even one not owned by the script.</p>
<p><strong>The Close button (in MsgBox's title bar)</strong>: Since the MsgBox window is a built-in feature of the operating system, its <strong>X</strong> button is enabled only when certain buttons are present. If there is only an OK button, clicking the <strong>X</strong> button is the same as pressing OK. Otherwise, the X button is disabled unless there is a Cancel button, in which case clicking the <strong>X</strong> is the same as pressing Cancel.</p>
<h2 id="Related">相关</h2>
<p><a href="InputBox.htm">InputBox</a>, <a href="FileSelect.htm">FileSelect</a>, <a href="DirSelect.htm">DirSelect</a>, <a href="ToolTip.htm">ToolTip</a>, <a href="../objects/Gui.htm">Gui object</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>
MsgBox  <em>; "Press OK to continue."</em>

MsgBox "This is a string."

MsgBox "This MsgBox has a custom title.", "A Custom Title"
</pre>
</div>

<div class="ex" id="ExContSec">
<p><a href="#ExContSec">#2</a>: Use a <a href="../Scripts.htm#continuation">continuation section</a> to span multiple lines:</p>
<pre>
MsgBox "
  (
    The first parameter is displayed as the message.
    The second parameter becomes the window title.
    The third parameter determines the type of message box.
  )", "Window Title", "iconi&quot;
</pre>
</div>

<div class="ex" id="ExRetValue">
<p><a href="#ExRetValue">#3</a>: Perform different actions depending on the return value:</p>
<pre>
if MsgBox("Do you want to continue? (Press YES or NO)",, "YesNo") = "No"
    return

result := MsgBox("This MsgBox will time out in 5 seconds.  Continue?",, "Y/N T5")
if result = "Timeout"
    MsgBox "You didn't press YES or NO within the 5-second period."
else if result = "No"
    return
</pre>
</div>

<div class="ex" id="ExExpr">
<p><a href="#ExExpr">#4</a>: Include a variable or sub-expression in the message:</p>
<pre>
var := 10
MsgBox "The initial value is: " var
MsgBox "The result is: " var * 2  <em>; <a href="../Variables.htm#concat">Concatenation</a>.</em>
MsgBox <a href="Format.htm">Format</a>("The result is: {1}", var * 2)</pre>
</div>

</body>
</html>